package com.jsms.api;

import cn.jiguang.common.resp.APIConnectionException;
import cn.jiguang.common.resp.APIRequestException;
import com.jsms.api.common.SMSClient;
import com.jsms.api.model.RecipientPayload;
import com.jsms.api.model.ScheduleListResult;
import com.jsms.api.model.ScheduleResult;
import com.jsms.api.model.ScheduleSMSPayload;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * Hello world!
 *
 */
public class JSMSTempleUtils {
    protected static final Logger LOG = LoggerFactory.getLogger(JSMSTempleUtils.class);

    protected static final String APP_KEY ="f19c5462045ae94eefb44563";
    protected static final String MASTER_SECRET = "6de11ca30113c0cda42074c2";
    private SMSClient client = null;

    public static void main(String []args){
        ScheduleResult result= new   ScheduleResult();
        result=JSMSTempleUtils.sendTemplateSMS("18600946089",137718,"hello",false,"2017-08-07 11:30:00");
        System.out.println(result);
    }
    public static ScheduleResult sendTemplateSMS(String phone, int tempId,String text,boolean isSendTime,String sendtime) {
        SMSClient client = new SMSClient(MASTER_SECRET, APP_KEY);
        ScheduleResult res;
        Date date=new Date();
        SimpleDateFormat dateFormater = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        ScheduleSMSPayload payload = ScheduleSMSPayload.newBuilder()
                .setSendTime(sendtime)
                .setMobileNumber(phone)
                .setTempId(tempId)
                .addTempPara("number", text)
                .build();
        ScheduleSMSPayload payload2= ScheduleSMSPayload.newBuilder()
                .setMobileNumber(phone)
                .setSendTime(dateFormater.format(date))
                .setTempId(tempId)
                .addTempPara("number", text)
                .build();
        try {
            if(isSendTime==true){
                res = client.sendScheduleSMS(payload);
            }else{
                res = client.sendScheduleSMS(payload2);
            }

            LOG.info(res.toString());
            return  res;
        } catch (APIConnectionException e) {
            LOG.error("Connection error. Should retry later. ", e);
        } catch (APIRequestException e) {
            LOG.error("Error response from JPush server. Should review and fix it. ", e);
            LOG.info("HTTP Status: " + e.getStatus());
            LOG.info("Error Message: " + e.getMessage());
        }
        return  null;
    }
    public static ScheduleListResult SendBatchScheduleSMS(List<RecipientPayload> list,boolean isSendTime,String sendTime,int tempId) {
        SMSClient client = new SMSClient(MASTER_SECRET, APP_KEY);
        RecipientPayload[] recipientPayloads = new RecipientPayload[list.size()];
        ScheduleListResult result;
        Date date=new Date();
        SimpleDateFormat dateFormater = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        ScheduleSMSPayload smsPayload1= ScheduleSMSPayload.newBuilder()
                .setSendTime(sendTime)
                .setTempId(tempId)
                .setRecipients(list.toArray(recipientPayloads))
                .build();
        ScheduleSMSPayload smsPayload2 = ScheduleSMSPayload.newBuilder()
                .setSendTime(dateFormater.format(date))
                .setTempId(tempId)
                .setRecipients(list.toArray(recipientPayloads))
                .build();
        try {
            if(isSendTime==true){
                 result = client.sendBatchScheduleSMS(smsPayload1);
            }else{
                result = client.sendBatchScheduleSMS(smsPayload2);
            }
            LOG.info(result.toString());
            return result;
        } catch (APIConnectionException e) {
            LOG.error("Connection error. Should retry later. ", e);
        } catch (APIRequestException e) {
            LOG.error("Error response from JPush server. Should review and fix it. ", e);
            LOG.info("HTTP Status: " + e.getStatus());
            LOG.info("Error Message: " + e.getMessage());
        }
        return null;
    }
    public static String getMessage(int code){
        String msg = "";
        switch (code) {
            case 50000:
                msg = "请求成功";
                break;
            case 50001:
                msg = "auth为空";
                break;
            case 50002:
                msg = "auth鉴权失败";
                break;
            case 50003:
                msg = "body为空";
                break;
            case 50004:
                msg = "手机号码为空";
                break;
            case 50005:
                msg = "模板Id为空";
                break;
            case 50006:
                msg = "手机号码无效";
                break;
            case 50007:
                msg = "body无效";
                break;
            case 50008:
                msg = "没有短信验证权限";
                break;
            case 50009:
                msg = "发送超频";
                break;
            case 50010:
                msg = "验证码无效";
                break;
            case 50011:
                msg = "验证码过期";
                break;
            case 50012:
                msg = "验证码已验证过";
                break;
            case 50013:
                msg = "无效模板id";
                break;
            case 50014:
                msg = "余额不足";
                break;
            case 50015:
                msg = "验证码为空";
                break;
            case 50016:
                msg = "api不存在";
                break;
            case 50017:
                msg = "媒体类型不支持";
                break;
            case 50018:
                msg = "请求方法不支持";
                break;
            case 50019:
                msg = "服务端异常";
                break;
            case 50020:
                msg = "模板审核中";
                break;
            case 50021:
                msg = "模板审核不通过";
                break;
            case 50022:
                msg = "模板参数未全部替换";
                break;
            case 50023:
                msg = "参数为空";
                break;
        }
        return msg;
    }
}
